/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ddbserver.common;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.Vector;

/**
 *
 * @author Roar
 */
public class ResultType implements Serializable{

    private String[] header;
    private Object[][] result;
    private Vector<String> columnHeaders = new Vector<String>();
    private Vector values = new Vector();
    private HashMap<String, Integer> joinMap = new HashMap<String, Integer>();
    private HashMap<String, String> tableAttrMap = new HashMap<String, String>();

    public void setHeader(String[] header) {
        this.header = header;
    }

    public void setColumnHeaders(Vector<String> columnHeaders) {
        this.columnHeaders = columnHeaders;
    }

    public void setResults(ResultSet rs, ResultSetMetaData rsmd, int recordsNumber) {
        int currentRow, currentCol;
        try{
            int headerNumber = rsmd.getColumnCount();
            header = new String[headerNumber];
            result = new Object[recordsNumber][headerNumber];
            for(currentCol = 0; currentCol < headerNumber; currentCol++){
                header[currentCol] = rsmd.getColumnName(currentCol + 1);
                currentRow = 0;
                if(rs.first()){
                    do {
                        result[currentRow][currentCol] = rs.getObject(currentCol + 1);
                        currentRow++;
                    }while(rs.next());
                }
            }
        } catch (Exception e) {
            ExceptionHandler.handleExcptin(e);
        }
    }

    public void setResutlVector(ResultSet rs){
        try{
            ResultSetMetaData rsmd = rs.getMetaData();
            int headerNumber = rsmd.getColumnCount();
            for(int i = 0; i < headerNumber; i++){
                if(rsmd.getColumnName(i + 1).equals("id")){
                    joinMap.put(rsmd.getTableName(i + 1), new Integer(i + 1));
                }
                tableAttrMap.put(rsmd.getTableName(i + 1), rsmd.getColumnName(i + 1));
                columnHeaders.addElement(new String(rsmd.getColumnName(i + 1)));
            }

            int count = 0;
            if(rs.first()){
                do{
                    Vector currentRow = new Vector();
                    for(int j = 0; j < headerNumber; j++){
                        if(rsmd.getColumnTypeName(j + 1).startsWith("CHAR") ) {
                            currentRow.addElement("\'" + rs.getObject(j + 1) + "\'");
                        } else {
                            currentRow.addElement(rs.getObject(j + 1));
                        }
                        
                    }
                    values.addElement(currentRow);
                    count++;
                }while(rs.next());
            }
        } catch (Exception e) {
            ExceptionHandler.handleExcptin(e);
        }
    }
    public String[] getHeader() {
        return header;
    }

    public Object[][] getValues() {
        return result;
    }

    public Vector getHeaderVector() {
        return columnHeaders;
    }

    public HashMap<String, Integer> getJoinMap() {
        return joinMap;
    }

    public HashMap<String, String> getTableAttrMap() {
        return tableAttrMap;
    }

    public Vector getValuesVector() {
        return values;
    }


}
